<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <!-- 权限点数据展示 -->
    <el-tree
      ref="tree"
      :data="permissionList"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    >
    </el-tree>
    <template #footer>
      <el-button @click="$emit('update:showAssignDialog', false)"
        >取消</el-button
      >
      <el-button type="primary" @click="savePermission">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionListApi } from '@/api/permission'
import { transListToTree } from '@/utils/index'
import { getRoleDetailApi, assignPermApi } from '@/api/setting'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      permissionList: [],
      roleId: ''
    }
  },
  created () {
    this.getPermissionList()
  },
  methods: {
    closeAssignDialog () {
      this.$emit('update:showAssignDialog', false)
      this.$refs.tree.setCheckedKeys([])
    },
    async getPermissionList () {
      const data = await getPermissionListApi()
      // console.log('权限列表', data)
      this.permissionList = transListToTree(data)
    },
    // 回显角色已有的权限
    async getRoleDetail (id) {
      this.roleId = id
      const { permIds } = await getRoleDetailApi(id)
      this.$refs.tree.setCheckedKeys(permIds)
    },
    async savePermission () {
      await assignPermApi({ id: this.roleId, permIds: this.$refs.tree.getCheckedKeys() })
      this.$message.success('操作成功')
      this.$emit('update:show-assign-dialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
